package com.example.administrator.test;


import java.io.File;
import java.io.IOException;

import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**所有同步操作均需new一个子线程。
 * Created by Administrator on 2017/3/27.
 */

public class OkHttpUtils {
    private OkHttpClient mOkHttpClient;
    private final MediaType MEDIA_JSON = MediaType.parse("application/json; charset=utf-8");
    private final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8");
    private final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
    private  final String IMGUR_CLIENT_ID = "...";
    private OkHttpUtils(){
        mOkHttpClient = new OkHttpClient();
    }
    public static OkHttpUtils getInstance(){
        return OkHttpUtilsInstance.mInstance;
    }
    static class OkHttpUtilsInstance{
        public static OkHttpUtils mInstance = new OkHttpUtils();
    }

    /**
     * 同步的网络请求
     * @param url
     * @return
     * @throws IOException
     */
    public Response getSyncResponse(String url) throws IOException {
        final Request request = new Request.Builder().url(url).build();
        return mOkHttpClient.newCall(request).execute();
    }

    /**
     * 异步网络请求
     * @param url
     * @return
     */
    public void getAsyncResponse(String url, Callback callback)
    {
        final Request request = new Request.Builder().url(url).build();
        mOkHttpClient.newCall(request).enqueue(callback);
    }

    /**
     * 同步网络请求 返回String，适用于返回数据<1M的情况
     * @param url
     * @return
     */
    public String getSyncString(String url) throws IOException {
        final Request request = new Request.Builder().url(url).build();
        return mOkHttpClient.newCall(request).execute().body().string();
    }

    /**
     * 同步上传Json
     * @param url 目标地址
     * @param json json数据
     * @return
     * @throws IOException
     */
    public String postSyncJson(String url,String json) throws IOException {
        RequestBody body = RequestBody.create(MEDIA_JSON,json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = mOkHttpClient.newCall(request).execute();
        return response.body().string();
    }

    /**
     * 异步上传Json
     * @param url
     * @param json
     * @param callback
     */
    public void postAsyncJson(String url,String json,Callback callback)
    {
        RequestBody body = RequestBody.create(MEDIA_JSON,json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        mOkHttpClient.newCall(request).enqueue(callback);
    }

    /**
     * 同步上传图片
     * @param imageForm
     * @return
     * @throws IOException
     */
    public String postSyncImage(ImageForm imageForm) throws IOException {
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("title", imageForm.title)
                .addFormDataPart("image", imageForm.name,
                        RequestBody.create(MEDIA_TYPE_PNG, imageForm.file))
                .build();
        Request request = new Request.Builder()
                .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
                .url(imageForm.url)
                .post(requestBody)
                .build();
        return  mOkHttpClient.newCall(request).execute().body().string();
    }

    /**
     * 异步上传图片
     * @param imageForm
     * @param callback
     */
    public void postAsyncImage(ImageForm imageForm,Callback callback)
    {
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("title", imageForm.title)
                .addFormDataPart("image", imageForm.name,
                        RequestBody.create(MEDIA_TYPE_PNG, imageForm.file))
                .build();
        Request request = new Request.Builder()
                .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
                .url(imageForm.url)
                .post(requestBody)
                .build();
        mOkHttpClient.newCall(request).enqueue(callback);
    }

    /**
     * 同步上传文件
     * @param file
     * @param url
     * @return
     * @throws IOException
     */
      public String postSyncFile(File file,String url) throws IOException {

          Request request = new Request.Builder()
                  .url(url)
                  .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
                  .build();
         return mOkHttpClient.newCall(request).execute().body().string();
      }

    /**
     * 异步上传文件
     * @param file
     * @param url
     * @param callback
     */
      public void postAsyncFile(File file,String url,Callback callback)
      {

          Request request = new Request.Builder()
                  .url(url)
                  .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
                  .build();
           mOkHttpClient.newCall(request).enqueue(callback);
      }

    public class ImageForm
    {
        public String url;
        public File file;
        public String title;
        public String name;
    }
}
